package edu.hqu.demo.controller;
import edu.hqu.demo.pojo.Course;
import edu.hqu.demo.service.CourseService;
import edu.hqu.demo.service.SelectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;

@Controller
public class SelectCourseController {

    String StudentNo;

    @Autowired
    private CourseService courseService;

    @Autowired
    private SelectionService selectionService;

    @RequestMapping("/courseShow")
    @ResponseBody
    public String courseList(@RequestParam String Sno){
        StudentNo = Sno.replace("\\","");
        StudentNo = StudentNo.replace("\"","");
        return "ok";
    }

    @RequestMapping("/goToSelectCourse")
    public String goToSelectCourse(Model model){
        List<Course> courseList = courseService.getAllCourse(StudentNo);
        model.addAttribute("courses",courseList);
        return "selectcourse";
    }

    @RequestMapping("/chooseCourse")
    @ResponseBody
    public String chooseCourse(@RequestParam String checkID,@RequestParam String Sno) {
        String newCheckID;
        String newSno;
        System.out.println(checkID);
        System.out.println(Sno);
        newCheckID = checkID.replace("[","");
        newCheckID = newCheckID.replace("]","");
        newCheckID = newCheckID.replace("\"","");

        newSno = Sno.replace("\\","");
        newSno = newSno.replace("\"","");

        String[] s = newCheckID.split(",");
        System.out.println(newCheckID);
        System.out.println(newSno);
        for (String string:s){
            selectionService.insertSelection(newSno,string);
        }
        return "ok";
    }

    @RequestMapping("/selectShow")
    public String selectShow() {
        return "selectionshow";
    }

}
